<?php

namespace Services\Sys\Models;

use Helpers\BaseModel;

class XhsysGrouppower extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iGroupId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mPowerTag;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mExtraView;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mExtraManage;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSys_GroupPower';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysGrouppower[]|XhsysGrouppower
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysGrouppower
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据组id判断是否有权限
     *
     * @param $groupId
     * @param $powerTag
     * @return bool
     *
     * create by ck 20171107
     */
    public static function checkPowerByTag($groupId, $powerTag)
    {
        static $powerArr, $flag = false;
        if(!$flag){
            $flag = true;
            $obj = self::findFirst($groupId);
            if (!$obj) {
                $powerArr = [];
            }
            else {
                $powerArr = explode(',', $obj->mPowerTag);
            }
        }
        return in_array($powerTag, $powerArr) ? true : false;
    }

}
